<!-- 房产中心 - 房产列表  -->
<template>

  <el-row :gutter="20">
    <!-- 左侧房产分类树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <CategoryTree @node-click="handleCategoryNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <!-- 搜索工作栏 -->
      <ContentWrap>
        <el-form
          ref="queryFormRef"
          :inline="true"
          :model="queryParams"
          class="-mb-15px"
          label-width="68px"
        >
          <el-form-item label="房产名称" prop="name">
            <el-input
              v-model="queryParams.name"
              class="!w-240px"
              clearable
              placeholder="请输入房产名称"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="房产分类" prop="categoryId">
            <el-cascader
              v-model="queryParams.categoryId"
              :options="categoryList"
              :props="defaultProps"
              class="w-1/1"
              clearable
              filterable
              placeholder="请选择房产分类"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery">
              <Icon class="mr-5px" icon="ep:search" />
              搜索
            </el-button>
            <el-button @click="resetQuery">
              <Icon class="mr-5px" icon="ep:refresh" />
              重置
            </el-button>
            <el-button
              v-hasPermi="['house:spu:create']"
              plain
              type="primary"
              @click="openForm(undefined)"
            >
              <Icon class="mr-5px" icon="ep:plus" />
              新增
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column align="center" label="房产名字" min-width="300" prop="name" />
          <el-table-column align="center" label="旧面积m²" min-width="160" prop="oldArea" />
          <el-table-column align="center" label="新面积m²" min-width="90" prop="area" />
          <el-table-column align="center" fixed="right" label="操作" min-width="200">
            <template #default="{ row }">
              <el-button link type="primary" @click="openDetail(row.id)"> 详情 </el-button>
              <el-button
                v-hasPermi="['house:spu:update']"
                link
                type="primary"
                @click="openForm(row.id)"
              >
                修改
              </el-button>
              <el-button
                v-hasPermi="['house:spu:delete']"
                link
                type="danger"
                @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          v-model:limit="queryParams.pageSize"
          v-model:page="queryParams.pageNo"
          :total="total"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>


</template>
<script lang="ts" setup>
import { defaultProps, handleTree } from '@/utils/tree'
import * as HouseSpuApi from '@/api/house/house/spu'
import * as HouseCategoryApi from '@/api/house/house/category'
import CategoryTree from "@/views/house/category/CategoryTree.vue"

defineOptions({ name: 'HouseSpu' })

const message = useMessage() // 消息弹窗
const route = useRoute() // 路由
const { t } = useI18n() // 国际化
const { push } = useRouter() // 路由跳转

const loading = ref(false) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref<HouseSpuApi.Spu[]>([]) // 列表的数据

const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  tabType: 0,
  name: '',
  categoryId: undefined,
  createTime: undefined
}) // 查询参数
const queryFormRef = ref() // 搜索的表单Ref

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await HouseSpuApi.getSpuPage(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await HouseSpuApi.deleteSpu(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 新增或修改 */
const openForm = (id?: number) => {
  // 修改
  if (typeof id === 'number') {
    push({ name: 'HouseSpuEdit', params: { id } })
    return
  }
  // 新增
  push({ name: 'HouseSpuAdd' })
}

/** 处理分类被点击 */
const handleCategoryNodeClick = async (row) => {
  queryParams.value.categoryId = row.id
  await getList()
}

/** 查看房产详情 */
const openDetail = (id: number) => {
  push({ name: 'HouseSpuDetail', params: { id } })
}

/** 获取分类的节点的完整结构 */
const categoryList = ref() // 分类树

/** 激活时 */
onActivated(() => {
  getList()
})

/** 初始化 **/
onMounted(async () => {
  // 解析路由的 categoryId
  if (route.query.categoryId) {
    queryParams.value.categoryId = Number(route.query.categoryId)
  }
  await getList()
  // 获得分类树
  const data = await HouseCategoryApi.getCategoryList({})
  categoryList.value = handleTree(data, 'id', 'parentId')
})
</script>
<style lang="scss" scoped>
.spu-table-expand {
  padding-left: 42px;

  :deep(.el-form-item__label) {
    width: 82px;
    font-weight: bold;
    color: #99a9bf;
  }
}
</style>
